package com.tjc.utils;

import java.util.Arrays;

/**
 * https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
 *
 * @Author: tjc
 * @description
 * @Date Created in 21:34 2022-10-13
 */
public class StringUtils {

    public static final String SPACE = " ";
    public static final String EMPTY = "";
    public static final String LF = "\n";
    public static final String CR = "\r";
    public static final String ELLIPSES = "...";
    public static final int INDEX_NOT_FOUND = -1;


    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.isEmpty();
    }

    public static String abbreviate(String str, int maxWidth) {
        if (isEmpty(str)) return str;
        int offset = 0;
        if (str.length() <= maxWidth) {
            return str;
        }
        if (ELLIPSES.length() >= maxWidth) {
            throw new IllegalArgumentException();
        }
        int to = offset + maxWidth - ELLIPSES.length();
        return str.substring(offset, to) + ELLIPSES;
    }


    public static String abbreviate(String str, int offset, int maxWidth) {
        return null;
    }


    public static String abbreviate(String str, String abbrevMarker, int maxWidth) {
        return null;
    }

    public static String abbreviate(String str, String abbrevMarker, int offset, int maxWidth) {
        return null;
    }

    public static String appendIfMissing(String str, CharSequence suffix, CharSequence... suffixes) {
        if (str == null) return null;
        if (suffix == null) return str;
        int subIndex = str.length() - suffix.length();
        if (subIndex < 0) {
            return str + suffix;
        }
        if (str.substring(subIndex).contentEquals(suffix)) {
            return str;
        }

        return str + suffix;
    }

    public static void main(String[] args) {
        String src = """
            StringUtils.appendIfMissing(null, null, null) = null
            StringUtils.appendIfMissing("abc", null, null) = "abc"
            StringUtils.appendIfMissing("", "xyz", null) = "xyz"
            StringUtils.appendIfMissing("abc", "xyz", new CharSequence[]{null}) = "abcxyz"
            StringUtils.appendIfMissing("abc", "xyz", "") = "abc"
            StringUtils.appendIfMissing("abc", "xyz", "mno") = "abcxyz"
            StringUtils.appendIfMissing("abcxyz", "xyz", "mno") = "abcxyz"
            StringUtils.appendIfMissing("abcmno", "xyz", "mno") = "abcmno"
            StringUtils.appendIfMissing("abcXYZ", "xyz", "mno") = "abcXYZxyz"
            StringUtils.appendIfMissing("abcMNO", "xyz", "mno") = "abcMNOxyz"
                """;
        String[] parts = src.split("\n");
        for (String part : parts) {
            String left = part.split("=")[0];
            String right = part.split("=")[1];
            System.out.println("assertEquals(" + right + "," + left + ");");
        }
    }

}
